package com.hailiang.study.javase.java.util;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 如果键存在, 该函数会修value改值;
 * 如果不存在, 它将放入键/值.
 */
public class Map_computeIfPresent {

    @Test
    public void test_computeIfPresent_01() {
        Map<String, String> presentMap = new HashMap<>();
        System.out.println(presentMap.computeIfPresent("language", (key, value) -> key + "：" + value)); //null -》key不存在，返回null,不做任何操作

        presentMap.put("language", "Java");
        System.out.println(presentMap.computeIfPresent("language", (key, value) -> key + "：" + value)); //null -》key不存在，返回null,不做任何操作
        System.out.println(presentMap);
        System.out.println(presentMap.computeIfPresent("language", (key, value) -> null)); //null -》key不存在，返回null,不做任何操作

        System.out.println(presentMap);

    }

}
